﻿using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.ClassFactory;
using IssueCommon;
using ValidateCommon;
using Common;
using BusinessLogic.BusinessRules;

namespace RichECMS.ServerPages.Template
{
    public partial class AddTemplate : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDateInTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
                LitParentPath.Text = State.LoginUser.TemplatePath.Replace(Server.MapPath(Request.Path).Replace("ServerPages\\Template\\UpdateTemplate.aspx", "").ToLower(), "");
                InitDataBind();
            }
        }

        /// <summary>
        /// 执行上传
        /// </summary>
        private string UploadExec()
        {
            if (FileUploadTemplate.FileName.Length != 0)
            {
                string strfilepath = State.LoginUser.TemplatePath + "\\" + FileUploadTemplate.FileName;
                FileUploadTemplate.SaveAs(strfilepath);
                return FileUploadTemplate.FileName;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 页面输出
        /// </summary>
        private void InitDataBind()
        {
            //绑定类型
            ListItem listitem = new ListItem("--请选择模版类型--", "-1");
            DropDownListType.Items.Add(listitem);
            foreach (int i in Enum.GetValues(typeof(Utility.TemplateType)))
            {
                listitem = new ListItem(Utility.GetTemplateType(i), i.ToString());
                DropDownListType.Items.Add(listitem);
            }
            ////绑定网站
            //listitem = new ListItem("--请选择站点--", "-1");
            //dropWebSiteList.Items.Add(listitem);
            //using (WebSiteFactory objFactory = new WebSiteFactory())
            //{
            //    BusinessLogic.BusinessRules.WebSite objWebSite = objFactory.Creator();
            //    if (objWebSite != null)
            //    {
            //        for (int i = 0; i < objWebSite.Count; i++)
            //        {
            //            listitem = new ListItem(objWebSite.DataItem.chvName, objWebSite.DataItem.intWebSiteID.ToString());
            //            dropWebSiteList.Items.Add(listitem);
            //        }
            //    }
            //}
            using (WebSiteFactory obgWebFactory = new WebSiteFactory())
            {
                BusinessLogic.BusinessRules.WebSite objWebsite = obgWebFactory.Creator(State.intWSID);
                if (objWebsite != null)
                {
                    LitWebTitle.Text = objWebsite.DataItem.chvTitle;
                }
                else
                {
                    Alert("该网站数据有误，请联系网站管理员");
                }
            }
        }

        /// <summary>
        /// 验证输入
        /// </summary>
        private bool ValidateValues()
        {
            bool bolIsValidate = true;
            if (this.txtTitle.Value.Trim().Length == 0)
            {
                Alert("请填写标题");
                bolIsValidate = false;
            }
            if (!ParamAuth.IsDateTime(SqlCommon.SqlStringFilter(txtDateInTime.Text)))
            {
                Alert("请输入正确的发布时间格式");
                bolIsValidate = false;
            }
            //if (dropWebSiteList.SelectedValue.Trim() == "-1")
            //{
            //    Alert("请选择站点");
            //    bolIsValidate = false;
            //}
            if (DropDownListType.SelectedValue.Trim() == "-1")
            {
                Alert("请选择模版类型");
                bolIsValidate = false;
            }

            return bolIsValidate;
        }

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            if (!ValidateValues())
            {
                return;
            }
            using (TemplateFactory objFactory = new TemplateFactory())
            {
                BusinessLogic.BusinessRules.Template objTemplate = objFactory.NewInstance();
                objTemplate.DataItem.chvName = this.txtTitle.Value;
                objTemplate.DataItem.intType = Convert.ToInt32(DropDownListType.SelectedValue.Trim());
                objTemplate.DataItem.intWSID = State.intWSID;
                objTemplate.DataItem.dtmInTime = Convert.ToDateTime(txtDateInTime.Text);
                objTemplate.DataItem.intState = Convert.ToInt32(RadioButtonListEnableUse.SelectedValue.Trim());
                if (FileUploadTemplate.FileName.Trim().Length != 0)
                {
                    objTemplate.DataItem.chvPath = UploadExec();
                }
                else
                {
                    objTemplate.DataItem.chvPath = this.txtTemplatePath.Value;
                }
                objTemplate.Insert();
            }
            AlertAndRedirect("新增成功！即将跳转，请稍候", "TemplateManage.aspx", false);
        }
    }
}